package binaryTree;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

public class Class637 {

    List<Double> list=new ArrayList<Double>();
    public List<Double> averageOfLevels(TreeNode root) {
        if (root == null) {
            return null;
        }
        Queue<TreeNode> queue = new LinkedList<TreeNode>();
        queue.add(root);
        while (!queue.isEmpty()) {
            int size = queue.size();
            int tempSum = 0;
            while (size > 0) {
                TreeNode peek = queue.peek();
                if (peek.left != null) {
                    queue.offer(peek.left);
                }
                if (peek.right != null) {
                    queue.offer(peek.right);
                }
                tempSum += queue.poll().val;
                size--;
            }
            list.add((double) (tempSum / size));
        }
return  list;
    }
}
